<?php
class Article_model extends CI_Model {
    private $_table = 'article';

    function __construct(){
        parent::__construct();
        $this->load->database();
    }

    function get_article($id = -1) {
	    $this->db->select('*');
	    $this->db->from($this->_table);
	    $this->db->where('id',$id);
	    $this->db->order_by("id","desc");
	    $query = $this->db->get();
	    $data = $query->result_array();
	    if($data != null)
	        return $data[0];
	    return null;
    }

	function updateArticle($data,$id){
		$this->db->where("id",$id);
		if($this->db->update($this->_table,$data))
			return TRUE;
		else
			return FALSE;
	}

    function getAllArticle( $offset = 0, $limit = 3){
        $this->db->select('*');
        $this->db->from($this->_table);
        $this->db->limit( $limit, $offset );
        $this->db->order_by("id","desc");
        $query = $this->db->get();
        $data = $query->result_array();
        return $data;
    }

	function getAllUserArticle($user_id = 1, $offset = 0, $limit = 3){
		$this->db->select('*');
		$this->db->from($this->_table);
		$this->db->where('userid',$user_id);
		$this->db->limit( $limit, $offset );
		$this->db->order_by("id","desc");
		$query = $this->db->get();
		$data = $query->result_array();
		return $data;
	}

	function getAllTitle($user_id = 1, $offset = 0, $limit = 3){
		$this->db->select('title');
		$this->db->from($this->_table);
		$this->db->where('userid',$user_id);
		$this->db->limit( $limit, $offset );
		$this->db->order_by("id","desc");
		$query = $this->db->get();
		$data = $query->result_array();
		return $data;
	}

	// Tong so record
	function num_rows(){
		return $this->db->count_all($this->_table);
	}

	function num_user_rows($id = 1){
		$this->db->where('userid',$id);
		return $this->db->count_all_results($this->_table);
	}
	//--- Xoa
	function deleteArticle($id){
		$this->db->where("id",$id);
		$this->db->delete($this->_table);
	}
}